package de.baumann.browser.views

import android.content.Context
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.RadioButton
import de.baumann.browser.R
import de.baumann.browser.utils.ViewUtil
import kotlinx.android.synthetic.main.layout_bottom_bar.view.*

@Deprecated("5.0.0版本删除")
class BottomBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : LinearLayout(context, attrs, defStyleAttr), View.OnClickListener {

    private var onBarClickListener: OnBarClickListener? = null
    private var lastCheckedRadio: RadioButton

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_bottom_bar, this)
        rbIndex.setOnClickListener(this)
        rbMenu.setOnClickListener(this)
        rbOdin.setOnClickListener(this)
        rbWindows.setOnClickListener(this)
        rbMine.setOnClickListener(this)

        ivBack.setOnClickListener(this)
        ivForward.setOnClickListener(this)
        ivMenu.setOnClickListener(this)
        flWindowsNum.setOnClickListener(this)
        ivHome.setOnClickListener(this)
        lastCheckedRadio = rbIndex
    }


    fun setTabCount(count: Int) {
        val normalDrawable = ViewUtil.drawText2Bitmap(count.toString(), R.drawable.ic_window_new_normal, context)
        val checkedDrawable = ViewUtil.drawText2Bitmap(count.toString(), R.drawable.ic_window_new_checked, context)
        val drawable = StateListDrawable()
//        drawable.addState(IntArray(android.R.attr.state_pressed), checkedDrawable)
        drawable.addState(IntArray(0), normalDrawable)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        rbWindows.setCompoundDrawables(null, drawable, null, null)
    }


    fun setOnBarClickListener(onBarClickListener: OnBarClickListener) {
        this.onBarClickListener = onBarClickListener
    }

    override fun onClick(v: View?) {
        if (onBarClickListener == null) {
            return
        }

        when (v?.id) {
            R.id.rbIndex -> {
                lastCheckedRadio = rbIndex
                onBarClickListener!!.onIndexClick()
            }
            R.id.rbMenu -> {
                onBarClickListener!!.onMenuClick()
            }
            R.id.rbOdin -> {
                lastCheckedRadio = rbOdin
                onBarClickListener!!.onOdinClick()
            }
            R.id.rbWindows -> {
                onBarClickListener!!.onWindowsClick()
            }
            R.id.rbMine -> {
                lastCheckedRadio = rbMine
                onBarClickListener!!.onMineClick()
            }

            R.id.ivBack -> onBarClickListener!!.onBackClick()
            R.id.ivForward -> onBarClickListener!!.onForwardClick()
            R.id.ivMenu -> onBarClickListener!!.onMenuClick()
            R.id.flWindowsNum -> onBarClickListener!!.onWindowsClick()
            R.id.ivHome -> onBarClickListener!!.onIndexClick()
        }
    }

    fun changeCheckedRadio() {
        lastCheckedRadio.isChecked = true
    }


    fun changeCheckedRadio(position: Int) {
        when (position) {
            0 -> lastCheckedRadio = rbIndex
            1 -> lastCheckedRadio = rbOdin
            2 -> lastCheckedRadio = rbMine
        }
        changeCheckedRadio()
    }


    interface OnBarClickListener {
        fun onIndexClick()
        fun onMenuClick()
        fun onOdinClick()
        fun onWindowsClick()
        fun onMineClick()

        fun onBackClick()
        fun onForwardClick()
    }


}
